package com.whw.cbf_qimodazuoye.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whw.cbf_qimodazuoye.R;
import com.whw.cbf_qimodazuoye.entity.User;
import com.whw.cbf_qimodazuoye.service.UserService;
import com.whw.cbf_qimodazuoye.utils.FaSongQingQiu;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity {

    EditText et_userId;
    EditText et_userPwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ImageView imageView = findViewById(R.id.userImage);
        imageView.setImageResource(R.drawable.user1);
        et_userId=(EditText) findViewById(R.id.userId);
        et_userPwd=(EditText) findViewById(R.id.userPwd);
        readAccount();
        Button btnlogin = (Button) findViewById(R.id.login);

         btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 String userId=et_userId.getText().toString();
                 String userPwd=et_userPwd.getText().toString();
                 login(userId,userPwd);
            }
        });



        Button btnregister = (Button) findViewById(R.id.register);
        btnregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
    public void readAccount(){
        SharedPreferences sharedPreferences = getSharedPreferences("info",MODE_PRIVATE);
        String userId = sharedPreferences.getString("userId","");
        String userPwd = sharedPreferences.getString("userPwd","");
        et_userId.setText(userId);
        et_userPwd.setText(userPwd);
    }

    public void login(String userId,String userPwd){
        Retrofit retrofit = FaSongQingQiu.createRetrofit();
        // 创建 网络请求接口 的实例
        UserService request = retrofit.create(UserService.class);
        //对 发送请求 进行封装
        Call<ResponseBody> call = request.login(userId,userPwd);
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //请求处理,输出结果
                try {
                    JSONObject jsonObject = (JSONObject) JSON.parse(response.body().string());
                    String msg =jsonObject.getString("msg");
                    String result = jsonObject.getString("result");
                    String returnobj = jsonObject.getString("obj");
                    JSONObject obj = (JSONObject)JSON.parse(returnobj);

                    if (result.equals("true")){
                        String userId =obj.getString("userId");
                        String userName =obj.getString("userName");
                        String userPwd = obj.getString("userPwd");
                        Integer userIntegral = Integer.parseInt(obj.getString("userIntegral"));
                        if (userId!=null){

                            CheckBox cb=(CheckBox)findViewById(R.id.cb);
                            if(cb.isChecked()){
                                SharedPreferences sp=getSharedPreferences("info",MODE_PRIVATE);
                                SharedPreferences.Editor ed=sp.edit();
                                ed.putString("userId",userId);
                                ed.putString("userPwd",userPwd);

                                ed.commit();
                            }
                            Bundle bundle = new Bundle();
                            bundle.putString("userName",userName);
                            System.out.println("真useriD："+userId);
                            bundle.putString("userId",userId);
                            bundle.putString("userIntegral",String.valueOf(userIntegral));
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtras(bundle);
                            startActivity(intent);
                            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                        }
                        else {
                            Toast.makeText(LoginActivity.this,"账号密码错误！",Toast.LENGTH_LONG).show();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //JsonObject jsonObject = new JsonObject()
            }
            //请求失败时候的回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("连接失败");
            }
        });
    }





}
